//树形dp - 树形dp入门
//1.树的深度
#include <iostream>
#include <vector>
using namespace std;
const int N = 1010;
int n;
vector<int> edges[N];
int f[N];

void dfs(int x)
{
    f[x] = 1;
    for(auto y : edges[x])
    {
        dfs(y); //先求出以y为根的子树的高度
        f[x] = max(f[x], f[y] + 1);
    }
}

int main()
{
    cin >> n;
    for(int i = 1; i < n; i++) 
    {
        int x, y; cin >> x >> y;
        edges[x].push_back(y);
    }
    dfs(1);
    cout << f[1] << endl;
    return 0;
}

//2.最大子树和
#include <iostream>
#include <vector>
using namespace std;
const int N = 16010;
vector<int> edges[N];
int n;
int a[N];
int f[N]; //f[x]表示以x为根的子树, 根节点必选, 此时的最大子树和

void dfs(int x, int fa)
{
    f[x] = a[x];
    for (auto y : edges[x])
    {
        if (y == fa) continue;
        dfs(y, x);
        if(f[y] > 0) 
            f[x] += f[y];
    }
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++)
    {
        int a, b; cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(1, 0);
    int ret = -0x3f3f3f3f;
    for (int i = 1; i <= n; i++)
        ret = max(ret, f[i]);
    cout << ret << endl;
    return 0;
}